package com.example.computer_exam_training_sys.service.impl;

import com.example.computer_exam_training_sys.entity.ExamPaper;
import com.example.computer_exam_training_sys.mapper.ExamPaperMapper;
import com.example.computer_exam_training_sys.service.ExamPaperService;
import com.example.computer_exam_training_sys.entity.adminlogsInfo;
import com.example.computer_exam_training_sys.mapper.admin_logsMapper;
import com.example.computer_exam_training_sys.service.admin_logsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ExamPaperServiceImpl implements ExamPaperService {

    @Autowired
    private ExamPaperMapper examPaperMapper;

    @Autowired
    private admin_logsMapper adminLogsMapper;

    @Override
    public int deleteUser(int paperId) {
        int result = examPaperMapper.deleteUser(paperId);
        if (result > 0) {
            // 添加日志记录
            adminlogsInfo log = new adminlogsInfo();
            log.setAdminId(1); // 假设当前管理员ID为1，实际应从上下文中获取
            log.setOperationType("删除试卷");
            log.setTargetId(paperId);
            log.setTargetType("试卷");
            log.setOperationDetail("删除了试卷ID为 " + paperId + " 的内容");
            log.setIpAddress("127.0.0.1"); // 假设IP地址为本地地址，实际应从请求中获取
            log.setCreateTime(LocalDateTime.now());
            adminLogsMapper.addLog(log);
        }
        return result;
    }

    @Override
    public List<ExamPaper> getAllExamPapers() {
        return examPaperMapper.selectAll();
    }

    @Override
    public ExamPaper addExamPaper(ExamPaper examPaper) {
        examPaperMapper.insert(examPaper);
        return examPaper;
    }
    @Override
    public ExamPaper getPaperById(Integer paperId) {
        return examPaperMapper.findById(paperId);
    }


    @Override
    public ExamPaper updateExamPaper(ExamPaper examPaper) {
        int result = examPaperMapper.update(examPaper);
        if (result > 0) {
            // 添加日志记录
            adminlogsInfo log = new adminlogsInfo();
            log.setAdminId(1); // 假设当前管理员ID为1，实际应从上下文中获取
            log.setOperationType("修改试卷");
            log.setTargetId(examPaper.getPaperId());
            log.setTargetType("试卷");
            log.setOperationDetail("修改了试卷ID为 " + examPaper.getPaperId() + " 的内容");
            log.setIpAddress("127.0.0.1"); // 假设IP地址为本地地址，实际应从请求中获取
            log.setCreateTime(LocalDateTime.now());
            adminLogsMapper.addLog(log);
        }
        return examPaper;
    }
}